﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HPortal.Lib;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;

namespace HPortal.Backend
{
    public partial class ArticleList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindcboChannel();
                if (cboChannelList.Items.Count > 0)
                    cboChannelList.SelectedValue = HtmlHelper.GetIntFromRequestString("ChannelID", 0).ToString();
                BindPanelList();
                if (cboPanelList.Items.Count > 0)
                {
                    cboPanelList.SelectedValue = HtmlHelper.GetIntFromRequestString("PanelID", 0).ToString();
                    BinhArticleList();
                }
            }
        }
        private DataTable getChannel()
        {
            try
            {
                string where = string.Empty;
                if (this.Context.User.Identity.IsAuthenticated)
                {
                    string[] arrRoleName = Roles.GetRolesForUser(this.Context.User.Identity.Name);
                    foreach (string roleName in arrRoleName)
                    {
                        where += DatabaseHelper.EscapeQuoteUnicode(roleName) + ",";
                    }

                }
                where = where.Remove(where.Length - 1);
                where = "WHERE ChannelID IN (SELECT DISTINCT ChannelID FROM [ChannelRole] WHERE ([Role] IN (" + where + ") AND (Edited=1 OR Approved=1 OR Administrated=1)))";
                string query = "SELECT * FROM [Channel] " + where + " ORDER BY [ParentID],[Priority],[ChannelID]";
                return DatabaseHelper.Select(query);
            }
            catch {
                return null;
            }
        }
        protected void BindcboChannel()
        {
            cboChannelList.DataSource = getChannel();
            cboChannelList.DataValueField = "ChannelID";
            cboChannelList.DataTextField = "Name";
            cboChannelList.DataBind();
            if (cboChannelList.Items.Count > 0)
                cboChannelList.SelectedIndex = 0;
        }
        protected void BindPanelList()
        {
            cboPanelList.DataSource = DatabaseHelper.Select("exec getPanelList " + cboChannelList.SelectedValue);
            cboPanelList.DataValueField = "PanelID";
            cboPanelList.DataTextField = "Header";
            cboPanelList.DataBind();
            if (cboPanelList.Items.Count > 0)
                cboPanelList.SelectedIndex = 0;
        }
        protected void cboChannelList_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindPanelList();
            BinhArticleList();
        }

        protected void cboPanelList_SelectedIndexChanged(object sender, EventArgs e)
        {
            BinhArticleList();
        }
        protected void BinhArticleList()
        {
            if (cboPanelList.Items.Count > 0)
            {
                grvArticlelList.DataSource = DatabaseHelper.Select("exec getArticleList " + cboPanelList.SelectedValue);
                grvArticlelList.DataBind();
            }
        }
        public string WriteArticleImage(object ImageType, int ArticleID_, string Url, int width)
        {

            if (ImageType != System.DBNull.Value)
            {
                return "<img src='/imgView.aspx?ArticleID=" + ArticleID_ + "' Width=" + width + "/>";
            }
            return string.Empty;
        }
    }
}